package mvc.views.components.main
{
	import alternativa.gui.alternativagui;
	import alternativa.gui.container.gridContainer.GridContainer;
	import alternativa.gui.container.linear.VBox;
	import alternativa.gui.container.linear.VBoxExpand;
	import alternativa.gui.enum.Align;
	import alternativa.gui.theme.defaulttheme.controls.buttons.CheckBox;
	import alternativa.gui.theme.defaulttheme.controls.text.TextArea;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	
	import mvc.models.VOs.AuthManageListVO;
	import mvc.models.proxys.ProxyAuth;
	
	import utils.UtilsUIManager;
	
	import xl.utils.UtilsGC;
	
	use namespace alternativagui;

	public class ContentAuthManage extends ContentBase
	{
		public static const NAME:String = 'ContentAuthManage';
		
		private static const COL_NUM:int = 5;
		private var mainContainer:VBoxExpand;
		private var authCheckBoxs:Vector.<CheckBox>;
		private var items:Dictionary;
		public function ContentAuthManage()
		{
			super();
			
			authCheckBoxs = new Vector.<CheckBox>();
		}
		
		override protected function initUI():void
		{
			// TODO Auto Generated method stub
			super.initUI();
			mainContainer = new VBoxExpand(10,Align.CENTER);
			addChild(mainContainer);
		}
		
		override public function update($model:Object):void
		{
			super.update($model);
			authCheckBoxs = new Vector.<CheckBox>();
			UtilsGC.clearChildren(mainContainer);
			items = new Dictionary(false);
			if($model==null)
			{
				ProxyAuth.getInstance().getAll();
			}
			else
			{
				var authManageListVO:Vector.<AuthManageListVO> = $model as Vector.<AuthManageListVO>;
				if(authManageListVO)
				{
					var len:int = authManageListVO.length;
					for(var i:int=0;i<len;++i)
					{
						var checkBox:CheckBox = getItem(authManageListVO[i]);
						mainContainer.addChild(checkBox);
						var children:Vector.<AuthManageListVO> = authManageListVO[i].children;
						var len2:int = children.length;
						if(len2>0)
						{
							var rows:int = Math.ceil(len2/COL_NUM);
							var grid:GridContainer = new GridContainer(COL_NUM,rows,10,10);
							grid.height = rows*20+10;
							var gridW:int = 0;
							items[checkBox]= new Vector.<CheckBox>(len2);
							for(var j:int=0;j<len2;++j)
							{
								var subCheckBox:CheckBox = getItem(children[j]);
								if(gridW<110*COL_NUM)
									gridW+=110;
								grid.addChild(subCheckBox);
								items[checkBox].push(subCheckBox);
							}
							
							grid.width =gridW;
							grid.freezeHeight=true;
							grid.freezeWidth=true;
							
							mainContainer.addChild(grid);							
						}

					}
					resize(_w,_h);
				}
			}
		}
		
		private function getItem($model:AuthManageListVO):CheckBox
		{
			var checkBox:CheckBox = UtilsUIManager.createCheckBox(null,$model.label,0,0,100,20);
			authCheckBoxs.push(checkBox);
			checkBox.addEventListener(MouseEvent.CLICK,onClick);
			return checkBox;
		}
		
		private function onClick(evt:Event):void
		{
			
		}
		
		override public function resize($w:int, $h:int):void
		{
			// TODO Auto Generated method stub
			super.resize($w, $h);
			mainContainer.resize($w,$w);
			//grid.resize(_w,_h);
		}
		
	}
}